package com.toommi.refuse.service.admin.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.toommi.refuse.entity.SysPermission;
import com.toommi.refuse.entity.SysPermissionExample;
import com.toommi.refuse.mapper.SysPermissionMapper;
import com.toommi.refuse.service.admin.SysPermissionService;
import lombok.AllArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author yangg
 * @since 2019-07-30 12:05
 */
@Service
@AllArgsConstructor
public class SysPermissionServiceImpl implements SysPermissionService {

    private SysPermissionMapper sysPermissionMapper;

    @Override
    public void addPermission(SysPermission permission) {
        permission.setCreateTime(System.currentTimeMillis());
        permission.setUpdateTime(System.currentTimeMillis());
        sysPermissionMapper.insert(permission);
    }

    @Override
    public void deletePermission(long id) {
        sysPermissionMapper.deleteByPrimaryKey(id);
    }

    @Override
    public PageInfo<SysPermission> findPermissions(int pageNum, int pageSize, String keyWord, String group) {
        //分页查询权限列表

        PageHelper.startPage(pageNum, pageSize);
        List<SysPermission> sysPermissions;
        if (StringUtils.isAllEmpty(keyWord, group)) {
            //查询全部
            sysPermissions = sysPermissionMapper.selectAll();
        } else {

            SysPermissionExample example = new SysPermissionExample();
            SysPermissionExample.Criteria criteria = example.createCriteria();
            if (StringUtils.isNotEmpty(keyWord)) {
                criteria.andPerssionNameLike("%" + keyWord + "%");
            }

            if (StringUtils.isNotEmpty(group)) {
                criteria.andGroupCodeEqualTo(group);
            }
            sysPermissions = sysPermissionMapper.selectByExample(example);
        }

        return new PageInfo<>(sysPermissions);
    }

    @Override
    public SysPermission findPermissionByCode(String code) {

        SysPermission sysPermission = new SysPermission();
        sysPermission.setPerssionCode(code);

        return sysPermissionMapper.selectOne(sysPermission);
    }
}
